/*
 * @Author: your name
 * @Date: 2021-11-19 15:46:58
 * @LastEditTime: 2021-11-19 16:58:32
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\serve\app\controller\file.js
 */
/*
 * @Author: your name
 * @Date: 2021-11-13 15:40:01
 * @LastEditTime: 2021-11-19 15:45:04
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\serve\app\controller\tagDb.js
 */
const fs = require("fs");
const path = require("path");
const pump = require("pump");
const Controller = require("../core/base_controller");
const timeFormart = require("../utils/timeFormart");
class FileController extends Controller {
    //上传图片
    async fileSetLoad() {
            const { ctx } = this;
            const date = Date.now();
            // 1.获取由前端传来的文件信息
            const stream = await ctx.getFileStream();
            //获取文件文件后缀名
            const extname = path.extname(stream.filename);
            // 2。根据前端输入的名字命名文件 toLocaleLowerCase:将大写字母转换为小写字母
            const filename =
                stream.filename.split(extname)[0] +
                Date.now(2) +
                extname.toLocaleLowerCase();
            // 3,创建文件的保存地址
            let target = path.join("app/public/static", filename);
            // 4,生成一个文件流
            const writeStream = fs.createWriteStream(target);
            // 5,将文件信息通过流,流入文件地址中
            await pump(stream, writeStream);
            // 6. 获取图片的服务器地址
            const imgUrl = ctx.origin + "/public/static/" + filename;
            //文件类型
            const file_type =
                "image/" +
                path.extname(stream.filename).toLocaleLowerCase().split(".")[1];
            //往数据库里添加图片各个信息--->(无添加图片大小) -->执行顺序
            await this.app.mysql.insert("files", {
                img_url: imgUrl,
                date: timeFormart(date), //
                path_url: target, //文件路径
                file_type: file_type, //文件类型
                file_name: stream.filename, //文件名称
            });
            //文件大小
            const file_size = fs.statSync(target).size;
            //转换为windows路径
            target = target.replace(/\\/g, "/");
            //获取数据库中数据
            const res = await this.app.mysql.get("files", { img_url: imgUrl });
            //更新数据库file_size属性
            if (res) {
                await this.app.mysql.update("files", {
                    id: res.id,
                    file_size: file_size,
                    path_url: target,
                });
            }
            ctx.body = {
                code: 200,
                url: imgUrl,
                path_url: target,
                date: timeFormart(date),
                file_type: file_type,
                file_size: file_size,
                file_name: stream.filename,
            };
        }
        //获取文件数据
    async getFileList() {
            const { ctx } = this;
            try {
                const res = await ctx.service.file.getFileList();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //删除文件
    async delFileList() {
        const { ctx } = this;
        try {
            const res = await ctx.service.file.delFileList();
            this.success(res);
        } catch (err) {
            this.error(err.message);
        }
    }
}

module.exports = FileController;